home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7901 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: ix.netcom.com!netnews
  2. From: Norman Bullen <nbullen@ix.netcom.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: very simple newbie question
  5. Date: Thu, 29 Feb 1996 06:34:47 -0800
  6. Organization: Black Cat Associates
  7. Message-ID: <3135B987.1DDE@ix.netcom.com>
  8. References: <robertk-2802960012350001@robertk.accessone.com> <TANMOY.96Feb28090444@qcd.lanl.gov>
  9. NNTP-Posting-Host: ple-ca9-19.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-NETCOM-Date: Thu Feb 29  6:43:39 AM PST 1996
  14. X-Mailer: Mozilla 2.0 (Win16; I)
  15. CC: robertk@accessone.com
  16.  
  17. Tanmoy Bhattacharya wrote:
  18. > In article <robertk-2802960012350001@robertk.accessone.com>
  19. > robertk@accessone.com (robertk) writes:
  20. > <snip>
  21. > r:              #include <stdio.h>
  22. > r:              main()
  23. > r:              {
  24. > r:               char s[10];
  25. > r:               scanf("%s/n",s);
  26. > r:               printf("%s",s);
  27. > r:              }
  28. > r:
  29. > r:             when i run this and input:
  30. > r:
  31. > r:                   robert <enter> --->when i hit the enter key the cursor
  32. > r:                                      drops down a line and sits
  33. > ther flashing
  34. > r:                                      waiting for me to enter
  35. > another character
  36. > r:                                      like x before it will throw me
  37. > back to the
  38. > r:                                      ide. upon returning to the
  39. > screen i see
  40. > r:                   robert
  41. > r:                   x
  42. > r:                   robert
  43. > r:
  44. > r:                i thought scanf() ignored whitespace?
  45. > First, /n is not white space, it is the character '/' followed by the
  46. > character 'n'. When /n appears as a part of the format string for
  47. > scanf, it tells scanf to read and discard the characters / and n if
  48. > they follow. As the end of line is not a /, the behaviour is not
  49. > consistent with your code. Check your code before wasting people's
  50. > time with it.
  51. > Next, whitespace is not ignored in scanf. It means read and discard
  52. > all following whitespace. So, scanf has to continue reading till it
  53. > finds a non-whitespace character, which it leaves unread. This
  54. > explains the behaviour that you see.
  55. > Cheers
  56. > Tanmoy
  57. > -- 
  58. What he's trying to say (but doesn't seem to express very well) is that 
  59. the representation of a "newline" character should have a backward slash 
  60. (the top end pointed left) instead of a forward slash.
  61.  
  62. That's not the real problem, however. scanf is a really poorly conceived 
  63. function, IMHO. It rules are too complicated and it does not do what most 
  64. people want it to do. For your situation, as shown in the sample code 
  65. above, you want to use gets instead of scanf.
  66.